<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2020/1/3
 * Time: 15:12
 */

namespace tools;
/**
 * @param $option
 * @return mixed
 */
class Curl
{
    protected $ch;
    protected $option;

    function __construct($option)
    {
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 29);
        $this->option = $option;

    }

    function send()
    {
        foreach ($this->option as $k => $v) {
            curl_setopt($this->ch, $k, $v);
        };
        $res = curl_exec($this->ch);
        curl_close($this->ch);
        return $res;
    }

    /**
     * 随机-设备
     */
    function round_equipment()
    {
        $array = array(
            'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36',
            'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
            'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36',
            'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
            'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586',
        );
        curl_setopt($this->ch, CURLOPT_USERAGENT, $array[rand(0, count($array) - 1)]);
        return $this;
    }

    /**
     * 随机ip
     */
    function round_ip()
    {
        $ip2id = round(rand(600000, 2550000) / 10000); //第一种方法，直接生成
        $ip3id = round(rand(600000, 2550000) / 10000);
        $ip4id = round(rand(600000, 2550000) / 10000);
        //下面是第二种方法，在以下数据中随机抽取
        $arr_1 = array("218", "218", "66", "66", "218", "218", "60", "60", "202", "204", "66", "66", "66", "59", "61", "60", "222", "221", "66", "59", "60", "60", "66", "218", "218", "62", "63", "64", "66", "66", "122", "211");
        $randarr = mt_rand(0, count($arr_1) - 1);
        $ip1id = $arr_1[$randarr];
        $ip = $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
        $this->option[CURLOPT_HTTPHEADER][] = 'X-FORWARDED-FOR:' . $ip;
        $this->option[CURLOPT_HTTPHEADER][] = 'CLIENT-IP:' . $ip;
        return $this;
    }

}
